package org.weasel.models;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.PlatformUI;
import org.weasel.business.Company;
import org.weasel.business.Iteration;
import org.weasel.business.Project;
import org.weasel.business.Release;
import org.weasel.controllers.PreferenceController;
import org.weasel.data.PersistencyAdapter;

public class Model {
	private Company company;
	private String path = "";
	private PersistencyAdapter persAd;
	private PreferenceController controller;
	
	public Model(){
		persAd = new PersistencyAdapter();
		//retrieve the path from the preferences
		this.controller = PreferenceController.instance();
	}
	
	public void save(){
		this.path = controller.getPrefStore().getString(PreferenceController.DATAFILELOC_PREFERENCE);
		try {
			persAd.save(this.path, this.company);
		} catch (Exception e) {
			MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Error", "There was an error when saving the data to the hard-disk! Please check your location of the datafile in the Preference menu under \"Window + Preferences\"!");
			e.printStackTrace();
		}
	}
	
	public void load() {
		this.path = controller.getPrefStore().getString(PreferenceController.DATAFILELOC_PREFERENCE);
		try {
			this.company = persAd.load(this.path);
			calculateDates();
		} catch (Exception e) {
			MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Error", "There was an error when loading the data from the hard-disk! Please check your location of the datafile in the Preference menu under \"Window + Preferences\"!");
			e.printStackTrace();
		}
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public Company getCompany() {
		return company;
	}
	
	public void calculateDates(){
		ArrayList<Project> projects = company.getProjects();
		for (Project project : projects) {
			if(!project.getStartDate().equals("")){
				int iterationDays = project.getIterationDays();
				//always assuming that each release has the same amount of iterations
				SimpleDateFormat bartDateFormat = new SimpleDateFormat("dd.MM.yyyy");
				Date startDate = null;
				try {
					startDate = bartDateFormat.parse(project.getStartDate());
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
	//			initializing date-formatter
				Calendar cal = Calendar.getInstance();
				//setting the date to the current
				cal.setTime(startDate);
				
				Date tmp = null;
				
				ArrayList<Release> releases = project.getReleases();
				for (Release release : releases) {
					tmp = cal.getTime();
					release.setStartDate(bartDateFormat.format(cal.getTime()));
					
					ArrayList<Iteration> iterations = release.getIterations();
					for (Iteration iteration : iterations) {
						cal.add(Calendar.DATE, iterationDays);
						iteration.setStartDate(bartDateFormat.format(tmp));
						iteration.setEndDate(bartDateFormat.format(cal.getTime()));
						tmp = cal.getTime();
					}
					release.setEndDate(bartDateFormat.format(tmp));
				}
				project.setEndDate(bartDateFormat.format(tmp));
			}
		}
	}
}
